<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    response.setHeader("X-Frame-Options", "SAMEORIGIN");
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <jsp:include page="../../public/public.jsp"/>
<body>
<!-- 菜单按钮、搜索框 -->
<div id="menu_tb" style="height:auto;margin-top: 20px;">
    <table cellspacing="0" cellpadding="0" style="border-bottom: 1px solid #cccccc" width="100%">
        <tr>
            <td style="height:50px;padding-left:20px">
                姓名:<input id="username" class="easyui-textbox" data-options="width:100"/>
                身份证号:<input id="idNum" class="easyui-textbox" data-options="width:100"/>
                <a href="javascript:void(0)" onclick="init()" class="easyui-linkbutton"
                   data-options="width:100,height:40,iconCls:'icon-search'">搜&nbsp;&nbsp;&nbsp;&nbsp;索</a>
                <a href="javascript:void(0)" onclick="chongzhi()" class="easyui-linkbutton"
                   data-options="width:100,height:40,iconCls:'icon-remove'">重&nbsp;&nbsp;&nbsp;&nbsp;置</a>
            </td>
        </tr>
    </table>

    <table style="height: auto;">
        <tr>
            <td>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-refresh" plain="true"
                   onclick="refresh()">刷新列表</a>
            </td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-edit" plain="true"
                   onclick="addEmployee()">新增从业人员</a>
            </td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-man" plain="true"
                   onclick="delEmployee()">删除从业人员</a>
            </td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
        </tr>
    </table>
</div>
<!-- end -->
<table id="dg" style="width:100%;height: 320px;" data-options="
				url:'<%=basePath %>employee/queryEmployee?userId=${sessionScope.userinfo.id}',
				rownumbers:true,
				singleSelect:true,
				autoRowHeight:false,
				pagination:true,
				pageSize:10">
    <thead>
    <tr>
        <th field="username" width="10%" align="center">姓名</th>
        <th field="sex" width="8%" align="center">性别</th>
        <th field="education" width="10%" align="center">学历</th>
        <th field="major" width="15%" align="center">专业</th>
        <th field="phoneNum" width="15%" align="center">电话号码</th>
        <th field="idNum" width="20%" align="center">身份证号</th>
        <th field="createTimeStr" width="15%" align="center">创建时间</th>
       <%-- <th field="obtainingCertificate" width="8%" align="center">获证</th>--%>
    </tr>
    </thead>
</table>

<!-- 添加窗口表单 -->
<div id="add" class="easyui-window" data-options="modal:true,closed:true,iconCls:'icon-edit'"
     style="width:300px;height:370px;padding:10px;">
    <form id="addForm" method="post">
        <table align="center" style="margin-top:20px">
            <tr style="height: 30px">
                <td>姓名:</td>
                <td><input name="username" id="usernameAdd" class="easyui-textbox"
                           data-options="width:150,required:true"/></td>
                <td>性别:</td>
                <td><input name="sex" id="sexAdd" class="easyui-textbox" data-options="width:150,required:true"/></td>
            </tr>
            <tr style="height: 30px">
                <td>学历:</td>
                <td><input name="education" id="educationAdd" class="easyui-textbox"
                           data-options="width:150,required:true"/></td>
                <td>专业:</td>
                <td><input name="major" id="majorAdd" class="easyui-textbox" data-options="width:150,required:true"/>
                </td>
            </tr>
            <tr style="height: 30px">
                <td>身份证号:</td>
                <td><input name="idNum" id="idNumAdd" class="easyui-textbox" data-options="width:150,required:true"/>
                </td>
            </tr>
                <td>手机号码:</td>
                <td><input name="phoneNum" id="phoneNumAdd" class="easyui-textbox" data-options="width:150,required:true"/>
                </td>
        </table>
    </form>
</div>
<!--添加窗口表单 end -->

<!-- 分配权限窗口 end -->
<script>
    $(function () {
        /* 分页 */
        var pager = $('#dg').datagrid().datagrid('getPager');

    })
    // 重新载入当前页面数据
    function refresh() {
        $('#dg').datagrid('reload');
    }

    //启用/删除
    function qidel(id, value) {
        var isDel = "";
        var delName = "";
        if (value == '1') {
            isDel = '2';
            delName = "禁用";
        }
        if (value == '2') {
            isDel = '1';
            delName = "启用";
        }

        $.messager.confirm('确认', '您确认想要 [ ' + delName + ' ] 数据吗？', function (r) {
            if (r) {
                $.ajax({
                    url: '<%=basePath %>user/updateUser',
                    type: 'post',
                    data: {
                        "id": id,
                        "isDel": isDel
                    },
                    dataType: 'json',
                    error: function () {
                        $.messager.alert("提示", delName + "异常", "error");
                    },
                    success: function (map) {
                        var row = map.row;
                        if (row >= 0) {
                            $.messager.alert("提示", delName + "成功", "info");
                            $('#dg').datagrid("reload");
                            return;
                        } else {
                            $.messager.alert("提示", delName + "失败", "error");
                            return;
                        }
                    }
                });
            }
        });
    }
    /* 点击搜索时触发 */
    function init() {
        var delName = "查询用户";
        var username = $("#username").val().replace(/\s/g, "");//登录账户
        var idNum = $("#idNum").val().replace(/\s/g, "");//显示名字
        $('#dg').datagrid({
            url: '<%=basePath %>employee/queryEmployee?dt=' + new Date().getTime(),
            queryParams: {
                userName: username,
                idNum: idNum
            }

        });
    }
    /* 点击重置时触发 */
    function chongzhi() {
        $('#username').textbox('clear');
        $('#idNum').textbox('clear');
    }
    /* 点击 新增用户时触发 */
    function addEmployee() {
        //先对表单进行清空
        $('#addForm').form('reset');
        $('#add').show();
        $('#add').dialog({
            title: '新增员工信息',
            width: 650,
            height: 350,
            closed: false,
            cache: false,
            modal: true,
            buttons: [{
                text: '保存',
                iconCls: 'icon-ok',
                handler: function () {
                    if ($("#addForm").form('validate')) {
                        var userNameAdd = $("#usernameAdd").val().replace(/\s/g, "")
                        if (userNameAdd == "") {
                            $.messager.alert("友情提示", "姓名不允许为空", "error");
                            return;
                        }

                        var showNameAdd = $("#idNumAdd").val().replace(/\s/g, "")
                        if (idNumAdd == "") {
                            $.messager.alert("友情提示", "身份证号不允许为空", "error");
                            return;
                        }
                        $.ajax({
                            url: '<%=basePath %>employee/insertEmployee',
                            type: 'post',
                            data: $("#addForm").serialize(),
                            dataType: 'json',
                            error: function () {
                                $.messager.alert("友情提示", "系统异常,请稍后尝试", "error");
                            },
                            success: function (data) {
                                var row = data.row;
                                var msg = data.msg;
                                $.messager.alert("友情提示", msg, "info");
                                if (row == 1) {
                                    $('#dg').datagrid("reload");
                                    $('#add').window('close');  // close a window
                                }
                            }
                        });
                    }

                }

            }, {
                text: '退出',
                iconCls: 'icon-no',
                handler: function () {
                    $('#add').dialog('close');
                }
            }]
        });
    }
    /* 点击删除获证员工出发 */
    function delEmployee() {
        var rowdata = $('#dg').datagrid('getSelected');
        if (rowdata == null) {
            $.messager.alert("友情提示", "请先选中一条数据", "error");
            return;
        }
        $.messager.confirm('确认', '您确认想要删除此从业人员的【全部数据】？', function (r) {
            if (r) {
                $.ajax({
                    url: '<%=basePath %>employee/delEmployee',
                    type: 'post',
                    data: {
                        'id': rowdata.id
                    },
                    dataType: 'json',
                    error: function () {
                        $.messager.alert("友情提示", "系统异常,请稍后尝试", "error");
                    },
                    success: function (data) {
                        var row = data.row;
                        var msg = data.msg;
                        $.messager.alert("友情提示", msg, "info");
                        if (row >= 0) {
                            $('#dg').datagrid("reload");
                        }
                    }
                });
            }
        });
    }
</script>
</body>
</html>